<?php
 
/**
 * This is the model class for table "asistencia".
 *
 * The followings are the available columns in table 'asistencia':
 * @property integer $id_clase
 * @property integer $id_alumno
 * @property integer $id_curso
 * @property boolean $presencia
 *
 * The followings are the available model relations:
 * @property AlumnoXCurso $idAlumno
 * @property AlumnoXCurso $idCurso
 * @property Clase $idClase
 */
class Asistencia extends CActiveRecord {
 
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Asistencia the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
 
    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'asistencia';
    }
 
    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id_clase, id_alumno, id_curso', 'required'),
            array('id_clase, id_alumno, id_curso', 'numerical', 'integerOnly' => true),
            array('presencia', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id_clase, id_alumno, id_curso, presencia', 'safe', 'on' => 'search'),
        );
    }
 
    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'idAlumno' => array(self::BELONGS_TO, 'AlumnoXCurso', 'id_alumno'),
            'idCurso' => array(self::BELONGS_TO, 'AlumnoXCurso', 'id_curso'),
            'idClase' => array(self::BELONGS_TO, 'Clase', 'id_clase'),
        );
    }
 
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id_clase' => 'Id Clase',
            'id_alumno' => 'Id Alumno',
            'id_curso' => 'Id Curso',
            'presencia' => 'Presencia',
        );
    }
 
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
 
        $criteria = new CDbCriteria;
 
        $criteria->compare('id_clase', $this->id_clase);
        $criteria->compare('id_alumno', $this->id_alumno);
        $criteria->compare('id_curso', $this->id_curso);
        $criteria->compare('presencia', $this->presencia);
 
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
 
    //******************************************************************************************************************************************
    public function getDatosAlumno($alumno) {
 
        $soapClient = new SoapClient(Yii::app()->getParams()->webServiceUrl);
        $response = $soapClient->getAlumno($alumno);
 
        if (isset($response)) {
            $ci = (int) ($response['doc_nro']);
            $query = Usuario::model()->findByPk($ci);
 
            return $query['nombre'] . ' ' . $query['apellido'];
        } else {
            throw new CHttpException(404, 'No funciona el getAlumno($alumno) en alumnoxcurso.php getDatosAlumno()');
        }
    }
 
    //******************************************************************************************************************************************
    public function getPresencia($curso, $alumno, $clase, $hora) {
        $queryString = "select presencia from asistencia a where '" . $curso . "' = a.id_curso
                                and '" . $alumno . "' = a.id_alumno and a.id_clase = '" . $clase . "';";
 
        $query = Yii::app()->db->createCommand($queryString)->queryRow();
        if ($query['presencia'] >= $hora) {
            return true;
        } else if ($query['presencia'] < $hora) {
            return false;
        }
    }
  //******************************************************************************************************************************************  
    public function getPorcentajeAsistencia($idAlumno, $idCurso, $idClase) {
        $soapClient = new SoapClient(Yii::app()->getParams()->webServiceUrl);
        $periodo = $soapClient->getCurso($idCurso);
        $fecha_periodo = $soapClient->getPeriodo($periodo);
        $fecha_desde = $fecha_periodo['fecha_desde'];
        $queryString = "select fecha from clase where id_clase = $idClase;";
        $fecha_h = Yii::app()->db->createCommand($queryString)->queryRow();
       $fecha_hasta = $fecha_h['fecha'];
        $aux = 0;
        $alumnoXReduccion = $soapClient->getAlumnoXReduccion($idAlumno, $idCurso);
        $alumnoXReduccion = (float) ($alumnoXReduccion / 100);
        $totalHorasMateria = $soapClient->getCurso($idCurso); //15semanas
        $totalHorasMateria = $totalHorasMateria['cnt_horas_sem'];
        if ($alumnoXReduccion == 0)
            $totalHorasMateria = $totalHorasMateria;
        else
            $totalHorasMateria = $totalHorasMateria * $alumnoXReduccion;

        $queryString = "select sum(presencia) as horas from asistencia where 
                                id_alumno = $idAlumno and id_curso = $idCurso and 
                          id_clase in( select id_clase from clase where fecha between '$fecha_desde' and '$fecha_hasta');";

        $query = Yii::app()->db->createCommand($queryString)->queryRow();
        $aux = $aux + (((int) $query['horas'] / $totalHorasMateria) * 100);
        
        return (round($aux,2).'%');
    }
    
    
    //funcion que devuelve la matricula del alumno
public function getMatricula($idAlumno){
	$queryString = "select ci from alumno where id_alumno=$idAlumno";
	$query = Yii::app()->db->createCommand($queryString)->queryRow();
	$u = Usuario::model()->findByPk($query['ci']);
	$matricula = $u->codigoadm;
	/*Yii::log(CVarDumper::dumpAsString($queryString),'info','index asistencia');
	Yii::log(CVarDumper::dumpAsString($query['ci']),'info','index asistencia'); 
	Yii::log(CVarDumper::dumpAsString($matricula),'info','index asistencia');*/
	return $matricula;
}
 
}